package com.konsung.ksdc.manager.addr;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.AddrInfoNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.ToastUtils;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/5/11 0011.
 */
public class AddrActivity extends BaseActivity {
    public static final String EDIT_ADDR_TYPE = "edit_addr_type";
    public static final int TYPE_CREAT = 10010;
    public static final String ADDR_DATA = "addr_data";
    public static final int REFRESHADDRESS = 1;
    public static final int SHOWADDRESS = 2;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tab_tv_title)
    TextView tabTvTitle;
    @Bind(R.id.rl_title_container)
    RelativeLayout rlTitleContainer;
    @Bind(R.id.lv_addr)
    ListView lvAddr;
    @Bind(R.id.btn_creat_addr)
    Button btnCreatAddr;

    List<AddrInfoNetBean> addrInfoBeanList = new ArrayList<>();
    private List<AddrInfoNetBean> mDatas;
    private AddrItemAdapter adapter;

    @Override
    protected String activityDesc() {
        return getString(R.string.page_addr);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addr);
        ButterKnife.bind(this);

        initData();
        initView();
    }

    /**
     * init data
     */
    private void initData() {
        RequestUtils.findAddress(NetConfig.currentEmpid,
                new RequestCallback() {

                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtils.toastContent(AddrActivity.this,
                                getString(R.string.chat_unconnect));
                    }

                    @Override
                    public void onResponse(ResponseBean responseBean) {
                        mDatas = responseBean.getList(new
                                TypeToken<List<AddrInfoNetBean>>() {
                                }.getType());
                        handler.sendEmptyMessage(SHOWADDRESS);
                    }
                });

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REFRESHADDRESS:
                    loadData();
                    break;
                case SHOWADDRESS:
                    adapter = new AddrItemAdapter(AddrActivity.this, mDatas, handler);
                    lvAddr.setAdapter(adapter);
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 重新联网拿取数据 加载界面
     */
    private void loadData() {
        RequestUtils.findAddress(NetConfig.currentEmpid,
                new RequestCallback() {

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(ResponseBean responseBean) {
                        mDatas = responseBean.getList(new
                                TypeToken<List<AddrInfoNetBean>>() {
                                }
                                .getType());

                        adapter.setDatas(mDatas);

                    }
                });
    }

    /**
     * init view
     */
    private void initView() {
        tabTvTitle.setText(R.string.title_addr);

        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnCreatAddr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(AddrActivity.this, CreateAddrActivity
                        .class);
                intent.putExtra(EDIT_ADDR_TYPE, TYPE_CREAT);
                startActivity(intent);
            }
        });

        lvAddr.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AddrInfoNetBean addrInfoNetBean = (AddrInfoNetBean)
                        lvAddr.getAdapter().getItem(position);
                //数据是使用Intent返回
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra(ADDR_DATA, addrInfoNetBean);
                //设置返回数据
                AddrActivity.this.setResult(RESULT_OK, intent);
                finish();
            }
        });
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
        if (adapter != null && mDatas != null) {
            adapter.setDatas(mDatas);
        }
    }
}
